var pcMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.pantonecolor');
var yarnMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.yarn');
var colorMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.pantonecolor');
var fcMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.francecustomer');
var fMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.factory');
var ctMaterialType = new i18nMessage('../../servlet/CodeTableServlet?name=CtMaterialType');
var ctYarnType = new i18nMessage('../../servlet/CodeTableServlet?name=CtYarnType');
var ctYarnMaterial = new i18nMessage('../../servlet/CodeTableServlet?name=CtYarnMaterial');
var ctYarnType = new i18nMessage('../../servlet/CodeTableServlet?name=CtYarnType');
var ctYarnMeasure = new i18nMessage('../../servlet/CodeTableServlet?name=CtYarnMeasure');

var biYarnStore;
var biYarnGrid;
var biYarnWindow;
function locateYarn(rowIndex){
	selectedYarn = biYarnStore.getAt(rowIndex).data;
}
var selectYarn = function(selectCallback){
    if (!biYarnWindow) {
        biYarnWindow = new Ext.Window({
            title: message.get('message.combo.pleaseselect'),
            layout: 'fit',
            width: 960,
            height: 610,
            closeAction: 'hide',
            plain: true,
            modal: true,
            items: [{
                id: 'yarnPanel',
                region: 'center',
                bodyStyle: {
                    background: '#ffffff',
                    padding: '7px'
                },
                html: '<div id="biYarnDiv"></div>'
            }]
        });
    }
    biYarnWindow.show();
    
    if (biYarnGrid) {
		biYarnStore.reload();
		return;
	}
    biYarnStore = new Ext.data.Store({
        url: '../../servlet/BiYarnServlet?action=4',
        
        reader: new Ext.data.XmlReader({
            record: 'item',
            id: 'id',
            totalRecords: 'itemTotal'
        }, [ 'index', 'reference', 'ctYarnType', 'comment', 
        'ctYarnMeasure', 'yarnAccount',
        'ctYarnMaterial1','ctYarnMaterial2','ctYarnMaterial3','ctYarnMaterial4','ctYarnMaterial5', 'photoName', {
			name: 'biFactory.nameCN',
			mapping: 'factoryNameCN'
		}, {
			name: 'biFactory.nameEN',
			mapping: 'factoryNameEN'
		},'id']),
        
        remoteSort: true
    });
    
    biYarnStore.setDefaultSort('id', 'desc');
    
    var filters = new Ext.grid.GridFilters({
	  filters:[
	    {type: 'string',  dataIndex: 'reference'},
		{type: 'string', dataIndex: 'biFactory.nameCN'},
		{type: 'string', dataIndex: 'biFactory.nameEN'},
		{
		      type: 'list',  
		      dataIndex: 'ctYarnMaterial1', 
		      options: ctYarnMaterial.getCtArray()
	    },{
		      type: 'list',  
		      dataIndex: 'ctYarnMaterial2', 
		      options: ctYarnMaterial.getCtArray()
	    },{
		      type: 'list',  
		      dataIndex: 'ctYarnMaterial3', 
		      options: ctYarnMaterial.getCtArray()
	    },{
		      type: 'list',  
		      dataIndex: 'ctYarnMaterial4', 
		      options: ctYarnMaterial.getCtArray()
	    },{
		      type: 'list',  
		      dataIndex: 'ctYarnMaterial5', 
		      options: ctYarnMaterial.getCtArray()
	    },{
		      type: 'list',  
		      dataIndex: 'ctYarnType', 
		      options: ctYarnType.getCtArray()
	    },{
		      type: 'list',  
		      dataIndex: 'ctYarnMeasure', 
		      options: ctYarnMeasure.getCtArray()
	    },{
		      type: 'string',  
		      dataIndex: 'yarnAccount'
	    }
	]});
	
    function renderSample(value, p, record){
		if (record == '') {
			return '';
		}
        return String.format('<table width="20" style="background-color: #{0};"><tr><td>&nbsp;</td></tr></table>', value);
		
    }
				
    function renderSelectItem(value, p, record, rowIndex){
        //var selectReferenceCallback = function(id,reference, needle,ctMachineType, weight,front,back,
        //sleeves,bottomThrum,sleevesThrum,necklineThrum,frontPhotoName,backPhotoName,otherPhotoName){
        return String.format('<a href="javascript: locateYarn({10});selectYarnCallback(\'{0}\',\'{1}\',\'{2}\',\'{3}\',\'{4}\',\'{5}\',\'{6}\',\'{7}\',\'{8}\',\'{9}\',\'{10}\',\'{11}\',\'{12}\');biYarnWindow.hide();"><img src="../../images/icons/arrow_rotate_clockwise.png" title="' + message.get('message.list.select') + '" /></a>', 
								    record.data.id, 
								    record.data.reference,
								    record.data['biFactory.nameCN'],
		 							record.data.ctYarnMaterial1,
		 							record.data.ctYarnMaterial2,
		 							record.data.ctYarnMaterial3,
		 							record.data.ctYarnMaterial4,
		 							record.data.ctYarnMaterial5,
		 							record.data.ctYarnMeasure, 
		 							record.data.yarnAccount,
		 							rowIndex);
    }
    // create the grid
    biYarnGrid = new Ext.grid.GridPanel({
        title: yarnMessage.get('ui.yarn.listtitle'),
        store: biYarnStore,
		stripeRows: true,
		plugins: filters,
        height: 560,
        columns: [{
            header: message.get('message.list.id'),
            width: 20,
            dataIndex: 'index',
            sortable: false
        },{
            header: yarnMessage.get('ui.yarn.type'),
            width: 80,
            dataIndex: 'ctYarnType',
            sortable: true
        },{
            header: message.get('message.edit.reference'),
            width: 80,
            dataIndex: 'reference',
            sortable: true
        },{
            header: yarnMessage.get('ui.yarn.yarnaccount'),
            width: 80,
            dataIndex: 'ctYarnMeasure',
            sortable: true
        },{
            header: '',
            width: 80,
            dataIndex: 'yarnAccount',
            sortable: true
        },{
            header: yarnMessage.get('ui.yarn.material') + '1',
            dataIndex: 'ctYarnMaterial1',
            sortable: true
        },{
            header: yarnMessage.get('ui.yarn.material') + '2',
            dataIndex: 'ctYarnMaterial2',
            sortable: true
        },{
            header: yarnMessage.get('ui.yarn.material') + '3',
            dataIndex: 'ctYarnMaterial3',
            sortable: true
        },{
            header: yarnMessage.get('ui.yarn.material') + '4',
            dataIndex: 'ctYarnMaterial4',
            sortable: true
        },{
            header: yarnMessage.get('ui.yarn.material') + '5',
            dataIndex: 'ctYarnMaterial5',
            sortable: true
        },{
            header: fMessage.get('ui.factory.name') + '(CN)',
            dataIndex: 'biFactory.nameCN',
            sortable: true
        },{
            header: fMessage.get('ui.factory.name') + '(FR/EN)',
            dataIndex: 'biFactory.nameEN',
            sortable: true
        }, {
            header: message.get('message.list.select'),
            width: 60,
            dataIndex: 'id',
            sortable: false,
            renderer: renderSelectItem
        }],
        viewConfig: {
            forceFit: true
        },
        loadMask: true,
        renderTo: 'biYarnDiv',
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: biYarnStore,
            displayInfo: true
        })
    });
    
    var startPage = getPrarmeterValue(window.location, 'startPage');
    var start;
    if (startPage == null || startPage == '') {
        start = 0;
    }
    else {
        start = (startPage - 1) * 20;
    }
    biYarnStore.load({
        params: {
            start: start,
            limit: 20
        }
    });
}

